package com.sduept.nwld.dataserver.interceptors;

import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import com.sduept.message.entity.AlarmMessage;
import com.sduept.message.entity.AlarmMessageCheck;
import com.sduept.message.manager.AlarmMessageCheckManager;
import com.sduept.nwld.dataserver.manager.protection.EquipmentTripDetailManager;
import com.sduept.nwld.dataserver.model.daily.Tripping;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import java.util.Date;
import java.util.List;

/**
 * @author Xiaopy
 * @version V1.0
 * @Date 2018年9月28日 上午11:36:23
 */
public class TzxxWarnInterceptor {

	@Autowired
	private EquipmentTripDetailManager detatilManager;
	@Autowired
	private ParamManager pc;
	@Autowired
	private AlarmMessageCheckManager messageCheckManager;
	@Autowired
	private SuperDAO dao;

	private static final String VOLTAGE_WARN = "220";

	@SuppressWarnings("unchecked")
	@AroundInvoke
	public Object methodInterceptor(InvocationContext ctx) throws Exception {

		List<Tripping> result = (List<Tripping>) ctx.proceed();
		for (Tripping trip : result) {
			String voltage = trip.getVoltageClass().replaceAll("[^\\d]", "");
			if (!VOLTAGE_WARN.equals(voltage)) {
				continue;
			}
			Date[] dates = getOneDates(trip.getTripTime());
			List<Tripping> list = detatilManager.listTrippings(trip.getLineName(), dates[0], dates[1]);
			int tempCount=warnCount();
			if (list.size() >= tempCount) {
				String sql = "select a.* from t_msg_alarm a left join t_oms_tzxx b on a.source=b.id where  a.msgType='tzgj'  and b.cim_xlmc=? and b.cim_fzsj between ? and ?  ";
				Object[] params = new Object[3];
				params[0] = trip.getLineName();
				params[1] = dates[0];
				params[2] = dates[1];
				List<AlarmMessage> tmpList = dao.nativeQuery(AlarmMessage.class, sql, params);
				String date = DateUtil.getStringDate(trip.getTripTime(), DateUtil.pattern_ymd);
				String content = trip.getLineName() + "/" + date + "跳闸次数达到" + list.size() + "次";
				if (tmpList.size() == 0) {
					// 生成告警
					AlarmMessage message = new AlarmMessage();
					message.setContent(content);
					message.setCreateTime(new Date());
					message.setMsgType(ParamDesc.TZGJ);
					message.setSource(trip.getId());
					message.setTimeliness(timeliness());
					message.setStatus(0);
					message.setPrimaryId(trip.getLineId());
					dao.create(message, AlarmMessage.class);
					messageCheckManager.createAlarmMessageCheckByAlarm(message);
					// 生成告警确认
				} else if (tmpList.size() == 1) {
					AlarmMessage message = tmpList.get(0);
					message.setContent(content);
					dao.createOrUpdate(message, AlarmMessage.class);
					AlarmMessageCheck messageCheck = messageCheckManager.getAlarmMessageCheckByMsgId(message.getId());
					if(null==messageCheck){
						messageCheckManager.createAlarmMessageCheckByAlarm(message);
						continue ;
					}
					messageCheck.setMsgContent(content);
					messageCheckManager.saveAlarmMessageCheck(messageCheck);
				}
			}

		}
		return null;
	}
	
	private int warnCount(){
		int result=3;
		SystemParameter parameter = pc.get(ParamDesc.TRIP_WARN_COUNT);
		if (null == parameter) {
			return result;
		}
		if (StringUtils.isNotEmpty(parameter.getValue())) {
			try {
				result = Integer.parseInt(parameter.getValue());
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return result;
	}
	private long timeliness() {
		long result = 259200000;
		SystemParameter parameter = pc.get(ParamDesc.TZGJ);
		if (null == parameter) {
			return result;
		}
		if (StringUtils.isNotEmpty(parameter.getValue())) {
			try {
				result = Long.parseLong(parameter.getValue());
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return result;
	}

	private Date[] getOneDates(Date dt) {
		Date[] result = new Date[2];
		String date = DateUtil.getStringDate(dt, DateUtil.pattern_ymd);
		String start = date + " 00:00:00";
		String end = date + " 23:59:59";
		result[0] = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		result[1] = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		return result;
	}
}
